package com.hod.client.frontend;

import java.util.Date;

import com.gwtext.client.core.Connection;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.TextAlign;
import com.gwtext.client.core.UrlParam;
import com.gwtext.client.data.BooleanFieldDef;
import com.gwtext.client.data.DateFieldDef;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.FloatFieldDef;
import com.gwtext.client.data.HttpProxy;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.data.XmlReader;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.DateField;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.grid.CellMetadata;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.EditorGridPanel;
import com.gwtext.client.widgets.grid.GridEditor;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.grid.GridView;
import com.gwtext.client.widgets.grid.Renderer;
import com.gwtext.client.widgets.grid.event.GridCellListenerAdapter;

public class EditableGrid extends EditorGridPanel {
	private SimpleStore cbStore = new SimpleStore("lightTypes", new String[]{
            "Shade",
            "Mostly Shady",
            "Sun or Shade",
            "Mostly Sunny",
            "Sunny"
    });
    private int width = 700;
    private int height = 500;
    private String title = "Remote Paging Grid";
	private final RecordDef recordDef = new RecordDef(
            new FieldDef[]{
                    new StringFieldDef("common"),
                    new StringFieldDef("botanical"),
                    new StringFieldDef("light"),
                    new FloatFieldDef("price"),
                    new DateFieldDef("availDate", "availability", "m/d/Y"),
                    new BooleanFieldDef("indoor")
            }
    );
	public Store configureStore() {
		// TODO Auto-generated method stub
		HttpProxy proxy = new HttpProxy("data/plants.xml", Connection.GET);
		XmlReader reader = new XmlReader("plant", recordDef);
        final Store store = new Store(proxy, reader);        
        store.load();
        cbStore.load();
		return store;
	}
	public GridView setGridView() {
		// TODO Auto-generated method stub
		return null;
	}
	public void setPagingToolbar() {
		// TODO Auto-generated method stub
		Toolbar toolbar = new Toolbar();

        ToolbarButton button = new ToolbarButton("Add Plant", new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {

                Record plant = recordDef.createRecord(new Object[]{"New Plant1", "Anguinaria Canadensis", "Mostly Shady", new Float(5), "", Boolean.FALSE});
                stopEditing();
                getStore().insert(0, plant);
                startEditing(0, 0);
            }
        });

        toolbar.addButton(button);
		
	}
	public ColumnModel setcolumnModel() {
		// TODO Auto-generated method stub
		final ComboBox cb = new ComboBox();
        cb.setDisplayField("lightTypes");
        cb.setStore(cbStore);
        
		ColumnConfig commonCol = new ColumnConfig("Common Name", "common", 220, true, null, "common");
        commonCol.setEditor(new GridEditor(new TextField()));

        ColumnConfig lightCol = new ColumnConfig("Light", "light", 130);
        lightCol.setEditor(new GridEditor(cb));


        ColumnConfig priceCol = new ColumnConfig("Price", "price", 70, true);
        priceCol.setAlign(TextAlign.RIGHT);
        priceCol.setRenderer(new Renderer() {
            public String render(Object value, CellMetadata cellMetadata, Record record, int rowIndex, int colNum, Store store) {
                return "$" + value;
            }
        });
        NumberField numberField = new NumberField();
        numberField.setAllowBlank(false);
        numberField.setAllowNegative(false);
        numberField.setMaxValue(1000);
        priceCol.setEditor(new GridEditor(numberField));

        ColumnConfig availableCol = new ColumnConfig("Available", "availDate", 95, true);

        DateField dateField = new DateField();
        dateField.setFormat("m/d/Y");
        dateField.setMinValue("01/01/06");
        dateField.setDisabledDays(new int[]{0, 6});
        dateField.setDisabledDaysText("Plants are not available on the weekend");
        availableCol.setEditor(new GridEditor(dateField));

        ColumnConfig indoorCol = new ColumnConfig("Indoor?", "indoor", 55);

        indoorCol.setRenderer(new Renderer() {
            public String render(Object value, CellMetadata cellMetadata, Record record, int rowIndex, int colNum, Store store) {
                boolean checked = ((Boolean) value).booleanValue();
                return "<img class=\"checkbox\" src=\"js/ext/resources/images/default/menu/" + (checked ? "checked.gif" : "unchecked.gif") + "\"/>";
            }
        });

        ColumnConfig[] columnConfigs = {
                commonCol,
                lightCol,
                priceCol,
                availableCol,
                indoorCol
        };

        ColumnModel columnModel = new ColumnModel(columnConfigs);
        columnModel.setDefaultSortable(true);
		
		return columnModel;
	}
	
	public EditableGrid(){
	   /*
		//super();
		setWidth(width);
        setHeight(height);
        setTitle(title);
        setClicksToEdit(1);
        setStore(configureStore());
        setColumnModel(setcolumnModel());
        setTrackMouseOver(false);
        setLoadMask(true);
        setSelectionModel(new RowSelectionModel());
        setFrame(true);
        setStripeRows(true);
        setIconCls("grid-icon");        
        //setView(setGridView());
        setPagingToolbar();
	    addGridCellListener(new GridCellListenerAdapter() {
           public void onCellClick(GridPanel grid, int rowIndex, int colIndex, EventObject e) {
               if (grid.getColumnModel().getDataIndex(colIndex).equals("indoor") && e.getTarget(".checkbox", 1) != null) {
                   Record record = grid.getStore().getAt(rowIndex);
                   record.set("indoor", !record.getAsBoolean("indoor"));
               }
           }
       });

       getStore().load(new UrlParam[]{new UrlParam("rnd", new Date().getTime() + "")});
	   */
		
        HttpProxy proxy = new HttpProxy("data/plants.xml", Connection.GET);

        final RecordDef recordDef = new RecordDef(
                new FieldDef[]{
                        new StringFieldDef("common"),
                        new StringFieldDef("botanical"),
                        new StringFieldDef("light"),
                        new FloatFieldDef("price"),
                        new DateFieldDef("availDate", "availability", "m/d/Y"),
                        new BooleanFieldDef("indoor")
                }
        );

        XmlReader reader = new XmlReader("plant", recordDef);
        final Store store = new Store(proxy, reader);
        SimpleStore cbStore = new SimpleStore("lightTypes", new String[]{
                "Shade",
                "Mostly Shady",
                "Sun or Shade",
                "Mostly Sunny",
                "Sunny"
        });

        store.load();
        cbStore.load();

        final ComboBox cb = new ComboBox();
        cb.setDisplayField("lightTypes");
        cb.setStore(cbStore);

        ColumnConfig commonCol = new ColumnConfig("Common Name", "common", 220, true, null, "common");
        commonCol.setEditor(new GridEditor(new TextField()));

        ColumnConfig lightCol = new ColumnConfig("Light", "light", 130);
        lightCol.setEditor(new GridEditor(cb));


        ColumnConfig priceCol = new ColumnConfig("Price", "price", 70, true);
        priceCol.setAlign(TextAlign.RIGHT);
        priceCol.setRenderer(new Renderer() {
            public String render(Object value, CellMetadata cellMetadata, Record record, int rowIndex, int colNum, Store store) {
                return "$" + value;
            }
        });
        NumberField numberField = new NumberField();
        numberField.setAllowBlank(false);
        numberField.setAllowNegative(false);
        numberField.setMaxValue(1000);
        priceCol.setEditor(new GridEditor(numberField));

        ColumnConfig availableCol = new ColumnConfig("Available", "availDate", 95, true);

        DateField dateField = new DateField();
        dateField.setFormat("m/d/Y");
        dateField.setMinValue("01/01/06");
        dateField.setDisabledDays(new int[]{0, 6});
        dateField.setDisabledDaysText("Plants are not available on the weekend");
        availableCol.setEditor(new GridEditor(dateField));

        ColumnConfig indoorCol = new ColumnConfig("Indoor?", "indoor", 55);

        indoorCol.setRenderer(new Renderer() {
            public String render(Object value, CellMetadata cellMetadata, Record record, int rowIndex, int colNum, Store store) {
                boolean checked = ((Boolean) value).booleanValue();
                return "<img class=\"checkbox\" src=\"js/ext/resources/images/default/menu/" + (checked ? "checked.gif" : "unchecked.gif") + "\"/>";
            }
        });

        ColumnConfig[] columnConfigs = {
                commonCol,
                lightCol,
                priceCol,
                availableCol,
                indoorCol
        };

        ColumnModel columnModel = new ColumnModel(columnConfigs);
        columnModel.setDefaultSortable(true);

        Toolbar toolbar = new Toolbar();

        ToolbarButton button = new ToolbarButton("Add Plant", new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {

                Record plant = recordDef.createRecord(new Object[]{"New Plant1", "Anguinaria Canadensis", "Mostly Shady", new Float(5), "", Boolean.FALSE});
                stopEditing();
                store.insert(0, plant);
                startEditing(0, 0);
            }
        });

        toolbar.addButton(button);

        setStore(store);
        setColumnModel(columnModel);
        setWidth(500);
        setHeight(300);
        setAutoExpandColumn("common");
        setTitle("Editor Grid Example");
        setFrame(true);
        setClicksToEdit(1);
        setTopToolbar(toolbar);

        addGridCellListener(new GridCellListenerAdapter() {
            public void onCellClick(GridPanel grid, int rowIndex, int colIndex, EventObject e) {
                if (grid.getColumnModel().getDataIndex(colIndex).equals("indoor") && e.getTarget(".checkbox", 1) != null) {
                    Record record = grid.getStore().getAt(rowIndex);
                    record.set("indoor", !record.getAsBoolean("indoor"));
                }
            }
        });

        store.load(new UrlParam[]{new UrlParam("rnd", new Date().getTime() + "")});
	}

}
